/*
 * Copyright Eclub organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.eclub.emotion.rootstone.serializer.scanning;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

import com.eclub.emotion.rootstone.server.ResourceFinder;

/**
 * 
 * A "jar" and "zip" scheme URI scanner that recursively jar files.
 *
 * @author   jun.yang, Wincor Nixdorf International GmbH
 * @version  $Revision$
 */
class JarZipSchemeResourceFinderFactory implements UriSchemeResourceFinderFactory {
	
	private Logger LOGGER = Logger.getLogger(JarZipSchemeResourceFinderFactory.class);
	
	public Set<String> getSchemes() {
        return new HashSet<String>(Arrays.asList("jar", "zip"));
    }

    JarZipSchemeResourceFinderFactory() {
    }

    public JarZipSchemeScanner create(final URI uri) {
        final String ssp = uri.getRawSchemeSpecificPart();
        final String jarUrlString = ssp.substring(0, ssp.lastIndexOf('!'));
        final String parent = ssp.substring(ssp.lastIndexOf('!') + 2);

        try {
            return new JarZipSchemeScanner(getInputStream(jarUrlString), parent);
        } catch (IOException e) {
            throw new ResourceFinderException(e);
        }
    }

    private class JarZipSchemeScanner implements ResourceFinder {

        private InputStream inputStream;
        private JarFileScanner jarFileScanner;

        private JarZipSchemeScanner(InputStream inputStream, String parent) throws IOException {
            this.inputStream = inputStream;
            this.jarFileScanner = new JarFileScanner(inputStream, parent);
        }

        @Override
        public boolean hasNext() {
            final boolean hasNext = jarFileScanner.hasNext();
            if(!hasNext) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                	LOGGER.error("Unable to close jar file.", e);
                }
                return false;
            }

            return true;
        }

        @Override
        public String next() {
            return jarFileScanner.next();
        }

        @Override
        public void remove() {
            jarFileScanner.remove();
        }

        @Override
        public InputStream open() {
            return jarFileScanner.open();
        }

        @Override
        public void reset() {
            jarFileScanner.reset();
        }

    }

    /**
     * Obtain a Inputstream of the jar file.
     * @return
     * @throws IOException
     */
    private InputStream getInputStream(String jarUrlString) throws IOException {
    	return new URL(jarUrlString).openStream();
    }
}


